//
//  HDAgentSelectVC.swift
//  HuaDaoPeifu
//
//  Created by ZERO on 2018/7/5.
//  Copyright © 2018年 paiyipai. All rights reserved.
//

import UIKit
import SVProgressHUD

class HDAgentSelectVC: UIViewController,SinglePickerViewDelegate {

    @IBOutlet weak var boardView: UIView!
    @IBOutlet weak var selectBtn: UIButton!
    @IBOutlet weak var startBtn: UIButton!
    @IBOutlet weak var agentLabel: UILabel!
    
    var InsuranceArray = [PYPInsuranceInfo]()
    var accountInfo:HDAccountInfo!

    fileprivate var agentPicker: SinglePickerView!
    fileprivate var selectedAgentCode : String?

    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        title = "机构选择";
        
        
        boardView.layer.borderWidth = 1
        boardView.layer.cornerRadius = 3
        boardView.layer.borderColor = UIColor(hex: 0xdbdbdb).cgColor
        
        selectBtn.layer.cornerRadius = 3
        selectBtn.layer.borderWidth = 1
        selectBtn.layer.borderColor = UIColor(red:(255/255.0), green:(138/255.0), blue:(0/255.0), alpha:1.0).cgColor
        
        startBtn.layer.cornerRadius = 3

        loadInsurance()
}
    
    
    func loadInsurance(){
        
        HDApiManagerHandler.requestInsurance { (result:HDApiResult?) in
            guard let result = result else {
                return
            }
            
            if (result.status == HDApiResultStatus.Success){
                
                guard let modelArray = result.data as? [Any] else {
                    return
                }
                self.agentPicker = SinglePickerView()
                self.agentPicker.arr = modelArray
                
                if modelArray.count > 0 {
                    let model = modelArray[0]
                    if let dict = model as? [String:String] {
                        self.agentLabel.text = dict["insurance_name"]
                        self.selectedAgentCode = dict["insurance_no"]                        
                    }
                }
                
                self.agentPicker?.type = SingleListType.agent;
                self.agentPicker?.delegate = self
            }
        }
    }
    @IBAction func selectAgentAction(_ sender: UIButton) {
        agentPicker?.showInView()
    }
    
    func complete(_ pickerView: UIView!, tempStr str: String!, idStr idstr: String!) {
            agentLabel.text = str
            selectedAgentCode = idstr
    }
    
    
// 重选身份
    @IBAction func selectAction(_ sender: UIButton) {
        HDAccountHandler.signOut()
        for vc in (navigationController?.viewControllers)! {
            if (vc is HDIdentitySelectVC){
                navigationController?.popToViewController(vc, animated: true)
            }
        }
    }
    
    @IBAction func startAction(_ sender: UIButton) {
        guard let insurance = selectedAgentCode else {
            HDIdentityTipsView.initHDIdentityTipsView(showMessage: "请选择投保机构")
            return
        }
        
        HDApiManagerHandler.requestGroupLogin(insuranceNo: insurance) {[unowned self] (result:HDApiResult?) in
            
            guard let result = result else {
                return
            }
            
            if(result.status == HDApiResultStatus.Success){
                self.loginSuccess()
                HDAccountHandler.saveUserInsuranceno(insurance)
                self.accountInfo.insuranceno = insurance
                HDAccountHandler.saveUserInfo(accountInfo: self.accountInfo)
            }else{
                HDIdentityTipsView.initHDIdentityTipsView(showMessage: result.msg)
            }
        }
    }
    
    func loginSuccess() {
        let a = UserDefaults.standard.bool(forKey: "Guide")
        if a {
            let vc = ViewController();
            UIApplication.shared.keyWindow?.rootViewController = vc;
            self.dismiss(animated: true, completion: nil)
            UserDefaults.standard.set(false, forKey: "Guide")
        }else{
            self.navigationController?.popToRootViewController(animated: true)
        }
    }


    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
}
